﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Churchtoolkit.Utils.MVP;
using Churchtoolkit.Web.Shell.Presenters;
using Churchtoolkit.Web.Shell.Views;
using System.Xml.Linq;
using System.Configuration;
using System.IO;
using System.Text;
using System.Drawing;
using System.Threading;
using Churchtoolkit.Utils.Navigation;

namespace Churchtoolkit.Web
{
    public partial class ChangeProfilePicture : Page<ChangeProfilePicturePresenter>, IChangeProfilePictureView
    {
        #region [ Data Members ]

        private string CurrentPicturePath
        {
            set
            {
                this.ViewState["CurrentPicturePath"] = value;
            }
            get
            {
                return this.ViewState["CurrentPicturePath"] as string;
            }
        }

        private string TempPicturePath
        {
            set
            {
                this.ViewState["TempPicturePath"] = value;
            }
            get
            {
                return this.ViewState["TempPicturePath"] as string;
            }
        }

        private string CurrentPicturePhysicalPath
        {
            get
            {
                if (!string.IsNullOrEmpty(CurrentPicturePath))
                {
                    return Server.MapPath(CurrentPicturePath);
                }
                return null;
            }
        }
        private string TempPicturePhysicalPath
        {
            get
            {
                if (!string.IsNullOrEmpty(TempPicturePath))
                {
                    return Server.MapPath(TempPicturePath);
                }
                return null;
            }
        }
        public DataAccess.Entities.Members CurrentMember
        {
            get { return this.Presenter.GetCurrentMember(); }
        }

        #endregion

        #region [ Events ]

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                PreviewCurrentProfilePicture();
            }
        }
        protected void btnUpload_Click(object sender, EventArgs e)
        {
            if (this.IsValid)
            {
                PreviewSelectedPicture();
            }
        }
        protected void btnOk_Click(object sender, EventArgs e)
        {
            if (this.IsValid)
            {
                SaveSelectedPicture();
            }
        }
        protected void btnCancel_Click(object sender, EventArgs e)
        {
            DeleteTempPicture();
        }
        protected void NotImage_Validate(object sender, ServerValidateEventArgs e)
        {
            if(!File.Exists(TempPicturePhysicalPath))
            {
                SaveTmpFile();
            }
            e.IsValid = IsImage(TempPicturePhysicalPath);
        }
        protected void BigImage_Validate(object sender, ServerValidateEventArgs e)
        {
            e.IsValid = NotBigImage(TempPicturePhysicalPath);
        }

        #endregion

        #region [ Helper ]

        private void PreviewCurrentProfilePicture()
        {
            string uploadsFolderPath = ConfigurationManager.AppSettings["UploadsFolder"];

            CurrentPicturePath = uploadsFolderPath + CurrentMember.ImagePath;

            TempPicturePath = uploadsFolderPath + CurrentMember.ImagePath + ".tmp";

            if (File.Exists(CurrentPicturePhysicalPath))
            {
                imgPreview.ImageUrl = CurrentPicturePath;
            }
            else
            {
                imgPreview.ImageUrl = @"~/Images/unknown-person.gif";
            }
        }
        private void SaveTmpFile()
        {
            fileUpChangePicture.SaveAs(TempPicturePhysicalPath);
        }
        private void PreviewSelectedPicture()
        {
            imgPreview.ImageUrl = TempPicturePath;
        }
        private bool NotBigImage(string imagePath)
        {
            if (File.ReadAllBytes(imagePath).Count() > 4096*1024)
            {
                return false;
            }
            return true;
        }
        private void SaveSelectedPicture()
        {
            if (File.Exists(TempPicturePhysicalPath))
            {
                File.Copy(TempPicturePhysicalPath, CurrentPicturePhysicalPath, true);
            }
        }
        private void DeleteTempPicture()
        {
            if (File.Exists(TempPicturePhysicalPath))
            {
                File.Delete(TempPicturePhysicalPath);
            }
        }
        private bool IsImage(string imagePath)
        {
            try
            {
                Bitmap image = new Bitmap(imagePath);
            }
            catch
            {
                return false;
            }
            return true;
        }

        #endregion
    }
}